package service;

import dao.BookingMapper;
import entity.Booking;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

public class BookingService {
    public List<Booking> listAll(String userName,Integer currPage,Integer pageSize){
        SqlSession sqlSession = null;
        List<Booking> list = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            int pageIndex=(currPage-1)*pageSize;
            list = sqlSession.getMapper(BookingMapper.class).listAll(userName,pageIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    public int count(){
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingMapper.class).count();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
    //删除
    public int delete(Integer id){
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            result = sqlSession.getMapper(BookingMapper.class).delete(id);
            //提交事物
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
    //新增
    public int add(Booking booking){
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            result=sqlSession.getMapper(BookingMapper.class).add(booking);
            //提交事物
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
    public int update(int id,int status){
        SqlSession sqlSession=null;
        int result=0;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            result=sqlSession.getMapper(BookingMapper.class).update(id,status);
            //提交事物
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return result;
    }
}
